package com.bluegalaxy.sitescore.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bluegalaxy.sitescore.common.enums.MessageType;
import com.bluegalaxy.sitescore.common.enums.RoleName;
import com.bluegalaxy.sitescore.common.enums.SourceType;
import com.bluegalaxy.sitescore.entity.TbMessage;
import com.bluegalaxy.sitescore.entity.TbSiteInfo;
import com.bluegalaxy.sitescore.entity.TbUser;
import com.bluegalaxy.sitescore.mapper.TbMessageMapper;
import com.bluegalaxy.sitescore.mapper.TbSiteInfoMapper;
import com.bluegalaxy.sitescore.mapper.TbUserMapper;
import com.bluegalaxy.sitescore.service.IRoleService;
import com.bluegalaxy.sitescore.service.ITbMessageService;
import com.bluegalaxy.sitescore.service.support.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TbMessageServiceImpl extends BaseServiceImpl<TbMessageMapper,TbMessage> implements ITbMessageService {

    @Autowired
    private TbMessageMapper tbMessageMapper;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private TbUserMapper userMapper;

    @Autowired
    private TbSiteInfoMapper siteInfoMapper;

    @Override
    public List<TbMessage> getMessageByUid(Long uid) {
        //获取该用户的角色名
        String roleName = roleService.selectRoleByUid(uid).getName();
        TbMessage tbMessage = new TbMessage();
        EntityWrapper<TbMessage> entityWrapper = new EntityWrapper<>();

        if(roleName.equals(RoleName.ADMINISTRATORS.getName())||roleName.equals(RoleName.AUDITOR.getName())){
            //审批人接收来自录入员的消息
            tbMessage.setSource(SourceType.BYENTRYCLERK.getKey());
            entityWrapper.setEntity(tbMessage);
            entityWrapper.notIn("read_status",MessageType.READ.getKey());
            List<TbMessage> messages =tbMessageMapper.selectList(entityWrapper);
            return messages;
        }else {
            tbMessage.setUserId(uid);
            //录入员接收来自审批人的消息
            tbMessage.setSource(SourceType.BYAUDITOR.getKey());
            entityWrapper.setEntity(tbMessage);
            entityWrapper.notIn("read_status",MessageType.READ.getKey());
            return tbMessageMapper.selectList(entityWrapper);
        }

    }

    @Override
    public boolean updateMessageRead(List<TbMessage> messageList) {
        for(TbMessage message : messageList){
            message.setRead_time(new Date());
            message.setRead_status(MessageType.READ.getKey());
        }
        tbMessageMapper.updateBatchById(messageList);
        return true;
    }

    @Override
    public boolean insertMessage(Long uid,Integer siteId) {
        //获取该用户的角色名
        String roleName = roleService.selectRoleByUid(uid).getName();
        //获取用户姓名
        TbUser user = new TbUser();
        user.setId(uid.intValue());
        String userName = userMapper.selectOne(user).getUsername();
        //获取场地名字,及场地所属用户
        TbSiteInfo siteInfo = new TbSiteInfo();
        siteInfo.setId(siteId);
        siteInfo = siteInfoMapper.selectOne(siteInfo);
        String siteName = siteInfo.getSiteName();
        //编辑信息
        TbMessage message = new TbMessage();
        message.setUserId(Long.valueOf(siteInfo.getCreate_by()));
        message.setCreate_time(new Date());
        message.setRead_status(MessageType.UNREAD.getKey());
        if(roleName.equals(RoleName.ADMINISTRATORS.getName())
                ||roleName.equals(RoleName.AUDITOR.getName())) {
            message.setSource(SourceType.BYAUDITOR.getKey());
            message.setMessage("场地"+siteName+"已审核");
        }else {
            message.setSource(SourceType.BYENTRYCLERK.getKey());
            message.setMessage(userName+"提交了新的场地"+siteName);
        }
        tbMessageMapper.insert(message);
        return true;
    }
}
